package com.zjitc.utils;


import com.zjitc.pojo.Product;

import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/11/6  15:11
 */
public class Utils {

  /**
   * 随机获取一个uuid，通用唯一的识别码(Universally Unique Identifier)
   *
   * @return uuid的string类型
   */
  public static String getUUID() {
    return UUID.randomUUID().toString().replace("-", "");
  }

  /**
   * 利用MD5进行加密
   *
   * @param password 待加密的字符串
   * @return 加密后的字符串
   * @throws NoSuchAlgorithmException 没有这种产生消息摘要的算法
   */
  public static String encoderByMd5(String password)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    try {
      //得到一个信息摘要器
      MessageDigest digest = MessageDigest.getInstance("md5");
      digest.update(password.getBytes());

      return new BigInteger(1, digest.digest()).toString(32);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static Cookie getCookieByName(String name, Cookie[] cookies) {
    if (cookies != null) {
      for (Cookie c : cookies) {
        if (name.equals(c.getName())) {
          return c;
        }
      }
    }
    return null;
  }


  /**
   * 随机生成快递单号
   */
  public static String code() {
    String s = "0123456789";
    Random random = new Random();

    String msg = "";
    for (int i = 0; i < MyNumber.TEN.ordinal(); i++) {
      int index = random.nextInt(10);
      String content = String.valueOf(s.charAt(index));
      msg += content;
      double theta = random.nextInt(45) * Math.PI / 180;
    }
    return msg;
  }

  /**
   * 模糊查询
   */
  public static <T> List fuzzysearch(String fuzzy, List<T> listGetAll) {
    return null;
  }

  /**
   *
   */
  public static String forgetcode() {
    String s = "ABCDEFGHGKLMNPQRSTUVWXYZ23456789";
    Random random = new Random();
    //session中要用到
    String msg = "";
    int x = 5;
    for (int i = 0; i < MyNumber.FIVE.ordinal(); i++) {
      int index = random.nextInt(32);
      String content = String.valueOf(s.charAt(index));
      msg += content;
    }
    return msg;
  }
}
